package com.example.controller.TestRedis;


import com.example.config.ErrorCodeEnum;
import com.example.controller.BaseController;
import com.example.util.RedisUtil;
import com.power.common.model.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import org.slf4j.*;

@Controller
@RequestMapping(value = "/testRedis")
@Api(tags = "测试Redis")
public class TestRedisController extends BaseController {

    private  static Logger logger = LoggerFactory.getLogger(TestRedisController.class);

    @Autowired
    private RedisUtil redisUtil;

//    尝试在其他控制器接收topic数据
    @JmsListener(destination="amq.topic")
    public void readActiveTopic2(String name) {
        System.out.println("在TestRedisController接收到topic消息：" + name);
    }

    @ApiOperation("添加redis数据")
    @RequestMapping(value = "/addRedis",method = {RequestMethod.GET})
    public void addRedis(String key, String value, HttpServletResponse response){
        try {
            redisUtil.set(key,value);
            this.writeJson(CommonResult.ok(ErrorCodeEnum.Add_SUCCESS),response);
        } catch (Exception e) {
            e.printStackTrace();
            this.writeJson(CommonResult.fail(ErrorCodeEnum.Add_FAil), response);
        }
    }

    @ApiOperation("删除数据")
    @RequestMapping(value = "/deleRedis",method = {RequestMethod.GET})
    public  void deleRedis(String key,HttpServletResponse response){
        try {

            if (redisUtil.hasKey(key)){
                redisUtil.del(key);
            }
            this.writeJson(CommonResult.ok(ErrorCodeEnum.Delete_SUCCESS),response);
        } catch (Exception e) {
            e.printStackTrace();
            this.writeJson(CommonResult.fail(ErrorCodeEnum.Delete_FAIL),response);
        }
    }

    @ApiOperation("修改数据")
    @RequestMapping(value = "/changeRedis",method = {RequestMethod.GET})
    public void changeRedis(String key,String value,HttpServletResponse response){
        try {
            if (redisUtil.hasKey(key)){
                redisUtil.set(key,value);
                this.writeJson(CommonResult.ok(ErrorCodeEnum.Change_SUCCESS), response);
            }else{
                this.writeJson(CommonResult.ok(ErrorCodeEnum.PARAM_EMPTY),response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            this.writeJson(CommonResult.fail(ErrorCodeEnum.Change_SUCCESS),response);
        }
    }

    @ApiOperation("查找数据")
    @ApiImplicitParam(name = "key",value = "存储的key",required = true)
    @RequestMapping(value = "/selectByKey",method = {RequestMethod.GET})
    public void selectByKey(String key, HttpServletResponse response){
        try {
            logger.info("参数:{}",key);
            Object value =  redisUtil.get(key);
            logger.info("内容{}",value);
            String content = "";
            if (value != null) {
                content = value.toString();
            }
            this.writeJson(CommonResult.ok(ErrorCodeEnum.Select_SUCCESS).setResult(content),response);
        } catch (Exception e) {
            e.printStackTrace();
            this.writeJson(CommonResult.ok(ErrorCodeEnum.Select_Fail),response);
        }
    }







}
